package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"math/rand"
	"strconv"
	"student-admin/app/models"
	"time"
)

func DefaultStudentList() []*models.Student {
	now := time.Now().Format("2006-01-02 15:04:05")
	return []*models.Student{
		{
			ID:        1,
			Username:  "tom",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市海淀区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        2,
			Username:  "lily",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市朝阳区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        3,
			Username:  "sam",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市东城区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        4,
			Username:  "tutu",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市西城区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        5,
			Username:  "kaka",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市昌平区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        6,
			Username:  "kiki",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市大兴区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        7,
			Username:  "rose",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市顺义区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        8,
			Username:  "jack",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市房山区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        9,
			Username:  "jim",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市石景山区",
			CreatedAt: now,
			UpdatedAt: now,
		},
		{
			ID:        10,
			Username:  "forward",
			Gender:    1,
			ClassID:   1,
			Address:   "北京市密云区",
			CreatedAt: now,
			UpdatedAt: now,
		},
	}
}

// GetStudentList 学生数据列表  数据仅仅模拟
func GetStudentList(c *gin.Context) {
	var pageNum, pageSize int64

	pageNum, _ = strconv.ParseInt(c.Query("page_num"), 10, 64)
	pageSize, _ = strconv.ParseInt(c.Query("page_size"), 10, 64)

	log.Println(pageNum, pageSize)
	stuList := DefaultStudentList()
	if pageNum == 11 {
		stuList = stuList[:3]
	}
	var data = struct {
		Total int64             `json:"total"`
		List  []*models.Student `json:"list"`
	}{
		Total: 103,
		List:  stuList,
	}
	for _, student := range stuList {
		student.ID += pageNum * 10000
		student.Username = fmt.Sprintf("%s-%d", student.Username, student.ID)
		student.ClassID = pageNum
		student.Gender = rand.Intn(2) + 1
	}
	ResponseJSON(c, 200, 0, "success", data)
}

func ResponseJSON(c *gin.Context, httpCode, code int, message string, data interface{}) {
	c.JSON(httpCode, gin.H{
		"code":    code,
		"message": message,
		"data":    data,
	})
}
